package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdUserOperationlogMapper;
import com.heima.admin.service.AdUserOperationlogService;
import com.heima.common.exception.CustException;
import com.heima.model.admin.dtos.AdUserlogDTO;
import com.heima.model.admin.pojo.AdUser;
import com.heima.model.admin.pojo.AdUserOperationlog;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.threadlocal.AdminThreadLocalUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class AdUserOperationlogServiceImpl extends ServiceImpl<AdUserOperationlogMapper, AdUserOperationlog> implements AdUserOperationlogService {
    @Autowired
    AdUserOperationlogMapper adUserOperationlogMapper;

    @Override
    public ResponseResult list(AdUserlogDTO dto) {
        //判断是否登录
        AdUser user = AdminThreadLocalUtils.getUser();
        if (user == null) {
            CustException.cust(AppHttpCodeEnum.NEED_LOGIN);
        }
        //进行条件查询
        LambdaQueryWrapper<AdUserOperationlog> query = Wrappers.lambdaQuery();
        if (dto.getName() != null) {
            query.like(AdUserOperationlog::getRequestPath, dto.getName());
        }
        if (dto.getStatus() != null) {
            query.like(AdUserOperationlog::getRequestMethod, dto.getStatus());
        }

        if (dto.getBeginDate() != null) {
            query.ge(AdUserOperationlog::getCreateTime, dto.getBeginDate());
        }
        if (dto.getEndDate() != null) {
            query.le(AdUserOperationlog::getCreateTime, dto.getEndDate());
        }
        //查询所有
        //List<AdUserOperationlog> adUserOperationlogs = adUserOperationlogMapper.selectList(query);
        //分页查询
        Page<AdUserOperationlog> reqPage = new Page<>(dto.getPage(),dto.getSize());
        IPage<AdUserOperationlog> page = page(reqPage, query);
        return new PageResponseResult(dto.getPage(), dto.getSize(), page.getTotal(), page.getRecords());
    }
}
